'use strict';

const Controller = require('egg').Controller;

class MessageController extends Controller {
  async index() {
    const {ctx} = this;
    const userId = ctx.user.id;
    // const userId = '62845b0b938430504d2eeac3';

    const [hasReadMessages, hasUnReadMessages] = await Promise.all([
      ctx.service.messageSvc.getReadMessagesByUserId(userId),
      ctx.service.messageSvc.getUnreadMessagesByUserId(userId),
    ]);

    // 把未读消息全部设置成已读
    await ctx.service.messageSvc.updateMessagesToRead(userId, hasUnReadMessages);
    await ctx.renderX('message/index.nj', {
      has_read_messages: hasReadMessages,
      hasnot_read_messages: hasUnReadMessages
    });
  }
}

module.exports = MessageController;

